package module{
	
	import component.Image;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import utils.Tween;
	
	public class Map extends Sprite{
		
		public static var instance:Map = new Map();
		
		private var blockList:Vector.<Image>;
		
		private var tWidth:int;
		private var tHeight:int;
		
		private const BLOCK_WIDTH:int = 197;
		private const BLOCK_HEIGHT:int = 144;
		
		private const LEAD_WIDTH:int = 20;
		private const LEAD_HEIGHT:int = 10;
		
		public var startPoints:Vector.<Point>;
		
		private var colIndex:int = 0;  //列下标
		private var rowIndex:int = 2;//行下标
		
		private const rotationXs:Array = [-30,0,30];
		
		public var tweenCount:int;
		
		
		public function Map(){
			this.blockList = new Vector.<Image>;
			this.startPoints = new Vector.<Point>;
			init();
		}
		
		private function init():void{
			for(var i:int = 0;i<9;i++){
				var block:Image = addChild(new Image()) as Image;
				blockList.push(block);
				var dx:int = (i%3)*(BLOCK_WIDTH + LEAD_WIDTH);
				var dy:int = int(i/3)*(BLOCK_HEIGHT + LEAD_HEIGHT);
				block.x = dx;
				block.y = dy;
				if((i+1)%3==0)startPoints.push(new Point(dx,dy));
			}
			this.tWidth = this.width;
			this.tHeight = this.height;
		}
		
		
		public function tweenGrid(bitmap:BitmapData):void{
			var block:Image = this.blockList.shift();
			block.bitmapData = bitmap;
			block.scaleX = BLOCK_WIDTH/bitmap.width;
			block.scaleY = BLOCK_HEIGHT/bitmap.height;
			var sX:int = startPoints[rowIndex].x;
			var sY:int = startPoints[rowIndex].y;
			var dX:int = colIndex*(BLOCK_WIDTH + LEAD_WIDTH);
			var dY:int = rowIndex*(BLOCK_HEIGHT + LEAD_HEIGHT);
			block.x = sX;
			block.y = sY;
			tweenCount++;
			Tween.to(block,750,{x:[sX,dX],y:[sY,dY],alpha:[0.6,1]});//
			rowIndex --;
			if(rowIndex < 0){
				rowIndex = 2;
				colIndex ++;
			}
			this.blockList.push(block);
			if(tweenCount==8)LoginPanel.instance.visible = true;
		}
		
		
		public function onResize():void{
			this.x = ((Global.stage.stageWidth - this.tWidth)>>1) - (LEAD_WIDTH+BLOCK_WIDTH) - BLOCK_WIDTH/2;
			this.y = ((Global.stage.stageHeight - this.tHeight)>>1) - BLOCK_HEIGHT/2;
		}
	}
}